package com.wudi.estate.controller.parkingmanagement;

import com.wudi.estate.bean.communitymanagement.Community;
import com.wudi.estate.bean.parkingmanagement.Parking;
import com.wudi.estate.service.communitymanagement.CommunityService;
import com.wudi.estate.service.parkingmanagement.ParkingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/parking")
public class ParkingController {
    @Autowired
    private ParkingService parkingService;
    @Autowired
    private CommunityService communityService;

    @RequestMapping("/listUi")
    public String listUi(ModelMap map){
        List<Map<String,Object>> parkings = null;
        try {
            parkings = parkingService.selectAll();
        } catch (Exception e) {
            e.printStackTrace();
        }
        map.addAttribute("parkings",parkings);
        return "parking_list";
    }
    @RequestMapping("/addUi")
    public String addUi(ModelMap map){
        try {
            List<Community> communities = communityService.select();
            System.out.println(communities);
            map.addAttribute("communities",communities);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "parking_add";
    }
    @RequestMapping("add")
    public String add(Parking parking){
        System.out.println(parking);
        try {
            parkingService.insert(parking);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "redirect:/parking/listUi";
    }
    @RequestMapping("/editUi")
    public String editUi(ModelMap map,String id){
        System.out.println(id);
        Map<String, Object> maps = null;
        try {
            maps = parkingService.selectParkingAndCommunity(id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        map.addAttribute("maps",maps);
        System.out.println(maps);
        return "parking_edit";
    }
    @RequestMapping("/edit")
    public String edit(Parking parking){
        System.out.println(parking);
        try {
            parkingService.update(parking);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "redirect:/parking/listUi";
    }
    @RequestMapping("/delete")
    public String delete(String id){
        try {
            parkingService.delete(id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "redirect:/parking/listUi";
    }
}

